package cn.itcast.sy7.listener;

import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

@WebListener

public class MyAttributeListener implements ServletContextAttributeListener, HttpSessionAttributeListener, ServletRequestAttributeListener {

    // ServletContext 属性添加
    public void attributeAdded(ServletContextAttributeEvent sae) {
        String name = sae.getName();
        Object value = sae.getServletContext().getAttribute(name);
        System.out.println("ServletContext添加属性：" + name + "=" + value);
    }

    // ServletContext 属性移除
    public void attributeRemoved(ServletContextAttributeEvent sae) {
        String name = sae.getName();
        System.out.println("ServletContext移除属性: " + name);
    }

    // ServletContext 属性替换
    public void attributeReplaced(ServletContextAttributeEvent sae) {
        String name = sae.getName();
        Object newValue = sae.getServletContext().getAttribute(name);
        System.out.println("ServletContext替换属性：" + name + "=" + newValue);
    }

    // HttpSession 属性添加
    public void attributeAdded(HttpSessionBindingEvent hbe) {
        String name = hbe.getName();
        Object value = hbe.getSession().getAttribute(name);
        System.out.println("HttpSession添加属性：" + name + "=" + value);
    }

    // HttpSession 属性移除
    public void attributeRemoved(HttpSessionBindingEvent hbe) {
        String name = hbe.getName();
        System.out.println("HttpSession移除属性: " + name);
    }

    // HttpSession 属性替换
    public void attributeReplaced(HttpSessionBindingEvent hbe) {
        String name = hbe.getName();
        Object newValue = hbe.getSession().getAttribute(name);
        System.out.println("HttpSession替换属性：" + name + "=" + newValue);
    }

    // ServletRequest 属性添加
    public void attributeAdded(ServletRequestAttributeEvent sra) {
        String name = sra.getName();
        Object value = sra.getServletRequest().getAttribute(name);
        System.out.println("ServletRequest添加属性：" + name + "=" + value);
    }

    // ServletRequest 属性移除
    public void attributeRemoved(ServletRequestAttributeEvent sra) {
        String name = sra.getName();
        System.out.println("ServletRequest移除属性: " + name);
    }

    // ServletRequest 属性替换
    public void attributeReplaced(ServletRequestAttributeEvent sra) {
        String name = sra.getName();
        Object newValue = sra.getServletRequest().getAttribute(name);
        System.out.println("ServletRequest替换属性：" + name + "=" + newValue);
    }
}